/*
 * @Author: xzy 1453077527@qq.com
 * @Date: 2025-08-25 14:05:13
 * @LastEditors: xzy 1453077527@qq.com
 * @LastEditTime: 2025-08-26 17:04:46
 * @Description:
 */
package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.RoleDao;
import com.xtedu.examproject.entity.Role;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    @Override
    public List<Role> findRolesByMenuId(int MenuId) throws Exception {
        String sql = "select r.* from role_menu rm left join roles r on rm.role_id = r.id where rm.menu_id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, MenuId);
            ResultSet rs = pstmt.executeQuery();
            List<Role> roles = new ArrayList<>();
            while (rs.next()) {
                Role role = new Role();
                role.setId(rs.getInt("id"));
                role.setRoleName(rs.getString("role_name"));
                role.setState(rs.getBoolean("state"));
                roles.add(role);
            }
            return roles;
        }
    }

    @Override
    public int insert(Role role) throws Exception {
        String sql = "insert into roles(role_name, state) values(?,?)";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, role.getRoleName());
            ps.setBoolean(2,role.isState());
            return ps.executeUpdate();
        }
    }

    @Override
    public int update(Role role) throws Exception {
        String sql = "update roles set role_name=?, state=? where id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, role.getRoleName());
            ps.setBoolean(2,role.isState());
            ps.setInt(3, role.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int delete(Role role) throws Exception {
        String sql = "delete from roles where id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, role.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public Role findById(int id) throws Exception {
        String sql = "select * from roles where id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            Role role = new Role();
            if (rs.next()) {
                role.setId(rs.getInt("id"));
                role.setRoleName(rs.getString("role_name"));
                role.setState(rs.getBoolean("state"));
            }
            return role;
        }
    }

    @Override
    public List<Role> findAll() throws Exception {
        String sql = "select * from roles";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            List<Role> roles = new ArrayList<>();
            while (rs.next()) {
                Role role = new Role();
                role.setId(rs.getInt("id"));
                role.setRoleName(rs.getString("role_name"));
                role.setState(rs.getBoolean("state"));
                roles.add(role);
            }
            return roles;
        }
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        String sql = "select * from roles LIMIT ?, ?";
        String countSql = "select count(*) from roles";
        try(
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
                PreparedStatement countPs = conn.prepareStatement(countSql);
                ){
            ps.setInt(1, (pageindex - 1) * pagesize);
            ps.setInt(2, pagesize);
            ResultSet rs = ps.executeQuery();
            List<Role> roles = new ArrayList<>();
            while (rs.next()) {
                Role role = new Role();
                role.setId(rs.getInt("id"));
                role.setRoleName(rs.getString("role_name"));
                role.setState(rs.getBoolean("state"));
                roles.add(role);
            }
            ResultSet countRs = countPs.executeQuery();
            int totalCount = 0;
            if (countRs.next()) {
                totalCount = countRs.getInt(1);
            }
            int totalPages = (totalCount + pagesize - 1) / pagesize;
            return new PageHelper<>(roles, pageindex, pagesize,totalPages, "/role/",  totalCount);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }

    public boolean deleteRoleById(int id) throws  Exception{
        String sql = "delete from roles where id=?";
        try(
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
                ){
            ps.setInt(1, id);
            return ps.executeUpdate() > 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据ID查到的信息来修改角色信息
     */
    public boolean updateRole(Role role) throws Exception {
        String sql = "UPDATE roles  SET role_name = ?, state = ? WHERE id = ?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setString(1, role.getRoleName());
            ps.setBoolean(2, role.isState());
            ps.setInt(3, role.getId());
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public List<Role> findAllRoleInfo() throws Exception {
        String sql = "select id,role_name from roles";
        try(Connection conn = DBUtil.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            List<Role> roles = new ArrayList<>();
            while (rs.next()) {
                Role role = new Role();
                role.setId(rs.getInt("id"));
                role.setRoleName(rs.getString("role_name"));
                roles.add(role);
            }
            return roles;
        }
    }
}
